In this lecture, we're going to keep blowing up teddy bears. But this time we'll use
a list of four loop and tags to simplify our
code. Let's get to work. As a reminder, we learned
about tags and unity, when we did the tag
destruction lecture in the selection module. The main camera is by default, tagged with the main camera tag, and our prefabs have
not been tagged at all. The explosion prefab is intact and the teddy bear
prefab is intact. Remember we can see
the tags we have available to us by
going to "edit", "project settings",
"tags and layers", and expanding tags
if you need to. As you can see, I've already added green, purple, and yellow tags. We're going to use tags
to make our code much more efficient when we
blow up teddy bears. Let's look at the
blowing up Teddy script. I have the same fields
that I had before. But my update method is much
simpler than it was before. Now, I'm just checking
to see if I have input on the blow-up
yellow teddies input axis. If I do, I call blowup teddies with my
yellow Teddy color. I do the same for blowing up green teddies and
blowing up purple teddies. As before, I have just
IF statements here, no else-if or else's, so that I can blow up multiple
colors on the same frame. My blowup teddies code is much simpler
than it was before. Instead of needing to check each GgmeObject in the scene first to see if it has
a sprite renderer, and then to see if it has
the appropriate color, all I need to do now
is the following. I still need to clear
my GameObjects list. This time, instead of finding all the game
objects in the scene, I'm going to find gameObjects
with a specific tag. The tag I'm going to look for is the color that was
passed in up here, which is the teddy
color enumeration. Tags are actually strings, not the teddy color enumeration, so I have to take that
enumeration value and convert it to a string by calling the ToString
method on it. This is a string that's either
green, purple, or yellow. Then I find all the game
objects that have been tagged, green, purple, or yellow. Then I add all those
elements of that array to my GameObjects list using
the average method. But now my GameObjects
list only contains GameObjects that are the color that I'm trying to blow up. I do my backwards for-loop, blowing up each element of that list and blow up
teddy stays the same. This code has been greatly
simplified by using tags. We have one more piece
to look at though. When we looked at the
teddy bear prefab in the Unity editor, it didn't have a tag. How is it getting a tag? I actually also showed you this in the tag destruction lecture. In our TeddyBear spawner script, when when I set the sprite
to a random sprite, I also set the tag. So here I've spawned the teddy bear and saved
that new GameObject. I've moved it to World position. I got it sprite renderer and based on the random
number I generated, I've set it sprite to
a particular color, or I've set it's sprite to a teddy bear sprite of
a particular color. Now I'm going to add a tag to
this teddy bear GameObject. I'm going to take Teddy
color dark green, which is the enumeration value green in the teddy
color enumeration, as I mentioned when we were looking at the blowing
up Teddy script, tags are strings,
not enumerations. I need to convert
this to a string, and this line of code
adds the green tag to the teddy bear I just instantiated
and gave green sprite. I do similar things for
purple and for yellow. The teddy bear spawner, when it randomly picks a TeddyBear colors bright to apply to the newly
spawned TeddyBear, it also adds a tag of
the appropriate color, and then in the blowing
up Teddy script, we use those tags to find the
teddy bears that we want to blow up based on a particular
input from the player. To recap, in this lecture, we re-factored our project from the previous lecture to use
tags to simplify our code.